package cn.e2win.biz.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysCompany;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.entity.SysUserRole;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAO;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.security.UserSession;


@Service
public class UserService extends BaseService{
	

	@Autowired
	private UserDao userDao;
	@Autowired
	private CompanyDao companyDao;
	
	@Override
	public BaseDAO initDao() {
		return userDao;
	}

	public void pagerUser(Pager pager){
		userDao.pagerUser(pager);
	}

	@Transactional(rollbackFor=Exception.class)
	public void assignRoles(Integer userId, Integer[] ids){
		userDao.delUserRoleByUserId(userId);
		
		//在页面中新增的
		List<SysUserRole> list = new ArrayList<SysUserRole>();
		for(Integer roleId : ids){
			list.add(new SysUserRole(userId, roleId));
		}
		userDao.saveAll(list);
	}
	public Map<String, Object> getUserForSession(Integer id){
		Map<String, Object> rMap = new HashMap<String, Object>();
		rMap = userDao.getUserInfo(id);
		return rMap;
	}

	@Transactional(rollbackFor=Exception.class)
	public void editUser(SysUser obj) throws MyException{
		SysUser user = UserSession.getUser();
		SysCompany company = (SysCompany)companyDao.getEntity(SysCompany.class, user.getCoId());
		/*
		List<SysRole> roles = new ArrayList<SysRole>();
		SysRole newRole = new SysRole();
		newRole.setId(obj.getLevelInt());
		roles.add(newRole);
		//obj.setRoles(roles);
		*/ 
		
		Long userCount = companyDao.userCount(user.getCoId());
		obj.setAccount(company.getAccountPrefix() + (userCount<10?("0" + (userCount+1)) : (userCount+1)));
		obj.setPassword(obj.getAccount());
		
		obj = (SysUser)super.edit(obj);
		
		
		
	}
	@Transactional(rollbackFor=Exception.class)
	public void delUser(SysUser obj,String tsf2ProcessorId) throws Exception {

		obj = (SysUser) getEntity(SysUser.class,obj.getId());
		SysUser newProcessor = (SysUser) getEntity(SysUser.class,Integer.parseInt(tsf2ProcessorId));
		System.err.println(obj.getCoId() + "=======" + newProcessor.getCoId());
		if(newProcessor==null || !obj.getCoId().equals(newProcessor.getCoId())){
			throw new MyException("移交客户到指定的业务员时出错：业务员不存在！" );			
		} else if(newProcessor.getActiveStatusInt()==0){
			throw new MyException("移交客户到指定的业务员时出错：业务员【" + obj.getUserName() + "】已经被作废！" );
		}
		
		obj.setActiveStatusInt(0);
		obj.setRemark("【删除用户】"+ obj.getRemark());
		super.delEntity(SysUser.class,obj.getId());
		
		int customerCount = userDao.tsfCustomerProcessor(obj.getId(), newProcessor.getId());
		System.err.println(obj.getActiveStatusInt() + "移交客户数量=======" + customerCount);
		
		
	}

	public void pagerLogs(Pager pager) {
		userDao.pagerLogs(pager);
	}
	

	public List<SysUserRole> getUserRoleListByUserId(Integer userId) {
		return userDao.getUserRoleListByUserId(userId);
	}

	public List<Map<String, Object>> getUserList(Map<String, Object> paramMap){
		return userDao.getUserList(paramMap);
	}
	
	public List<SysUser> getUserByOpenId(String openId) {
		return userDao.getUserByOpenId(openId);
	}
	@Transactional(rollbackFor=Exception.class)
	public void processOpenId4LoginAndOut(String openId, SysUser user, String inOrOut) throws MyException{
		System.err.println("=================================loginByOpenId  ======");
		List<SysUser> list = userDao.getUserByOpenId(openId);
		
		for(SysUser temp : list){
			if(temp.getId()!=user.getId()){
				System.err.println("remove openid========" + temp.getAccount());
				temp.setOpenId("");
			}
		}

		super.saveAll(list);
		if("LOGIN".equals(inOrOut)){
			user.setOpenId(openId);
			super.edit(user);
		}
		System.err.println("done ========");
	}
	

	public SysUser getUserByType(String userType,Integer id) throws Exception {return userDao.getUserByType(userType,id);}
}
